Yacc

%{
        #include<stdio.h>
 int aaa;

%}

%union {
                char dval;
        }

%token <dval> NUM
%token <dval> LETTER
%type <dval> E
%left '+' '-' 
%left '*' '/'

%%
statement : E {printf("\nt = %c\n This Three Address Code Generated..\n",$1);}
          ;

E : E '+' E {$$=codegen($$,$1,'+',$3);}
  | E '-' E {$$=codegen($$,$1,'-',$3);}
  | E '*' E {$$=codegen($$,$1,'*',$3);}
  | E '/' E {$$=codegen($$,$1,'/',$3);}
  | '(' E ')' {$$=$2;}
  | NUM {$$=$1;}
  |LETTER {$$=$1;}
  ;
%%


Lex

%{
        #include<stdio.h>
        #include"y.tab.h"
        char ch='a';

%}
%%
[0-9]+ {yylval.dval=yytext[0]; return NUM;}

[a-zA-Z]+ {yylval.dval=yytext[0]; return LETTER;}
\n {return 0;}
. {return yytext[0];}
%%
void yyerror(char* str)
{
        printf("\n%s",str);
}
char codegen(char ch1,char first,char op,char second)
{
        printf("\n%c = %c %c %c\n",ch,first,op,second);
        return ch++;
}
main()
{
        yyparse();
        return 0;
}
int yywrap()
{
return 1;
}

Output-

hacker@HaCkEr-PC:~/Desktop$ gedit new.y
hacker@HaCkEr-PC:~/Desktop$ lex new.l
hacker@HaCkEr-PC:~/Desktop$ yacc -d new.y
hacker@HaCkEr-PC:~/Desktop$ gcc lex.yy.c y.tab.c
hacker@HaCkEr-PC:~/Desktop$ ./a.out
x+y*(z+3)/6

a = z + 3

b = y * a

c = b / 6

d = x + c

t = d
 This Three Address Code Generated..
